package com.allwees.bs.module.ops.rest;

import com.allwees.bs.module.core.annotation.PrintLog;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.bs.module.core.rest.BoCrudApi;
import com.allwees.bs.module.ops.entity.AppVersion;
import com.allwees.bs.module.ops.req.AppVersionCrudReq;
import com.allwees.bs.module.ops.req.query.AppVersionQueryReq;
import com.allwees.bs.module.ops.service.AppVersionService;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 *
 * @author yangylsky
 * @since 2019-12-04
 *
 */
@PrintLog
@RestController
@RequestMapping("/backstage/api/app_version")
public class AppVersionApi extends BoCrudApi<AppVersion, AppVersionService> {
	@GetMapping("page")
	public R page(Page<AppVersion> page, AppVersionQueryReq req) {
		QueryWrapper<AppVersion> wrapper = new QueryWrapper<>();
		req.buildQuery(wrapper);
		return super.page(page, req);
	}

	@PutMapping("/update")
	public R update(@RequestBody AppVersionCrudReq req) {
		validateParam(req);
		boService.update(req);
		return R.ok();
	}

	@PostMapping("/save")
	public R save(@RequestBody AppVersionCrudReq req) {
		validateParam(req);
		boService.save(req);
		return R.ok();
	}



	@DeleteMapping("{uuid}")
	public R  delete(@PathVariable("uuid") String uuid) {
		Assert.isTrue(boService.findByUuid(uuid) != null , "record is not exist");
		boService.deleteByUuid(uuid);
		return R.ok();
	}

	private void validateParam(AppVersionCrudReq req){
		AppVersion appVersion = req.getBo();
		String minVersion = appVersion.getMinVersion();
		String latestVersion = appVersion.getLatestVersion();
		Assert.isTrue(minVersion  != null && minVersion.contains("."),"version format wrong");
		String[] minVersionArr = minVersion.split("\\.");
		Assert.isTrue(minVersionArr.length >= 3,"version format wrong");

		Assert.isTrue(latestVersion  != null && latestVersion.contains("."),"version format wrong");
		String[] latestVersionArr = latestVersion.split("\\.");
		Assert.isTrue(latestVersionArr.length >= 3,"version format wrong");
	}
}
